package com.xiaoyang.loginservice.controller;

import com.xiaoyang.loginservice.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.UUID;

/**
 * @auther XiaoYang
 * @DATE 2023/5/6-15:46
 */
@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/doLogin")
    public String doLogin(String name, String pwd) {
        System.out.println(name);
        System.out.println(pwd);
        //这里假设去做了登录
        User user = User.builder()
                .id(1)
                .name(name)
                .pwd(pwd)
                .age(18).build();


        String token = UUID.randomUUID().toString();

        //存起来                           key      value            过期时间7200s=2h
        redisTemplate.opsForValue().set(token,user.toString(), Duration.ofSeconds(7200));

        return "bearer "+token;
    }

}
